import { UserService } from './../../providers/UserService';
import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { StorageService } from '../../providers/StorageService';
import { BasePage } from '../basePage';

/**
 * Generated class for the InputtextPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-inputtext',
  templateUrl: 'inputtext.html',
})
export class InputtextPage extends BasePage {

  userinfo={}  //用户信息
  type = 'text';  //文本框的类型
  inputname = ""; //文本框的名称
  placeholder = ''; //文本框的提示字段
  fontnum: 8;     //文本框显示的个数
  count = 0;  //正在输入的个数
  inputvalue = '' //用户输入的值
  password:boolean;  //控制修改密码版块显示
  normal:boolean=true;  //正常情况下的版块
  origin="";  //旧密码
  new="";    //新密码
 


  constructor(injector: Injector,public stc:StorageService,public userService:UserService) {
    super(injector);
    this.userinfo = this.stc.read('userinfo');
    console.log(this.userinfo)
    if (this.navParams.get('inputname'))
      this.inputname = this.navParams.get('inputname');


    if (this.navParams.get('type'))
      this.type = this.navParams.get('type');

    if (this.navParams.get('placeholder'))
      this.placeholder = this.navParams.get('placeholder');

    if (this.navParams.get('fontnum'))
      this.fontnum = this.navParams.get('fontnum');

    if (this.navParams.get('inputvalue'))
      this.inputvalue = this.navParams.get('inputvalue');

    //判断是不是修改密码页面  
    if(this.inputname == "修改密码"){
      this.normal=false;
      this.password=true;
    }else{
      this.password=false;
      this.normal=true;
    }
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad InputtextPage');
    this.count = this.textlength(this.inputvalue);
  }

  /**计算用户输入值的长度*/
  onChangeinput() {
    this.count = this.textlength(this.inputvalue);
    console.log(this.count)
  }

  /**清空用户输入的值 */
  clearinput() {
    this.inputvalue = '';
    this.count = 0;
  }

  //字数限制
  private textlength(currentfield) {
    let len = 0;
    for (let i = 0; i < currentfield.length; i++) {
      let c = currentfield.charCodeAt(i);
      len++;
    }
    return len
  }

  /**完成文本框的编辑 */
  dismissokCLI() {
    this.viewCtrl.dismiss(this.inputvalue);
  }


  /**返回上一级目录 */
  dismissCLI() {
    this.viewCtrl.dismiss();
  }

  //判断输入的原密码是否与数据一致
  // checkpwd(){
  //   console.log(this.origin);
  //   if(this.origin != this.userinfo['user_pwd']){
  //     this.origin="";
  //     this.showAlert("密码错误，请重新输入");
  //     return false
  //   }
  // }
 
  async change(){
    //判断原密码是否正确
    if(this.origin != this.userinfo['user_pwd']){
      this.origin="";
      this.showAlert("密码错误，请重新输入");
      return false;
    }else{
      let reg = /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$).{6,12}$/;
      let res=reg.test(this.new);
      console.log(this.new)
      //判断新密码的格式是否合法
      if(res == false){
        this.showAlert("密码必须由6-12位英文字母、数字或符号组成");
        return false;
      }else{
        //准备请求
        let param = { 'user_id': this.userinfo['user_id'],'new_pwd':this.new,'old_pwd':this.userinfo['user_pwd'] };
        let loader = this.loadingCtrl.create({
          content: "",
        });
        loader.present();
        console.log(param)
        let result = await this.userService.editPwdAPI(param);
        loader.dismiss();
        //判断结果
        if (result.code == 1) {
          let toast = this.toastCtrl.create({
            message: result.msg,
            duration: 1600,
            position: "bottom"
          });
          toast.present();
          setTimeout(()=> {
            let modal = this.modalCtrl.create('LoginPage');
            modal.onDidDismiss(data => {
            });
            modal.present();
          }, 1600);
        } else {
          let toast = this.toastCtrl.create({
            message:'网络繁忙',
            duration: 2000,
            position: "bottom"
          });
          toast.present();
        }  
      }
    }
    
  }
}
